package com.article.service.Impl;


import cn.hutool.core.bean.BeanUtil;
import com.article.entity.dto.IdsDao;
import com.article.entity.pojo.ColumnList;
import com.article.entity.vo.ColumnListVo;
import com.article.mapper.ColumnMapper;
import com.article.service.ColumnService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ColumnServiceImpl extends ServiceImpl<ColumnMapper, ColumnList> implements ColumnService {

    @Autowired
    private ColumnMapper columnMapper;

    @Override
    public List<ColumnListVo> getColumn(int pid, int page, int size) {
        List<ColumnListVo> column = new ArrayList<>();
        int pageStart = (page - 1) * size;
        List<ColumnListVo> column1 = columnMapper.getColumn(pid, pageStart, size);

        for (ColumnListVo columnVo : column1) {
            List<ColumnListVo> children = getColumn(columnVo.getId(), page, size);
            columnVo.setChildren(children);
        }
        BeanUtil.copyProperties(column1, column);

        return column1;
    }

    @Override
    public Boolean insetColumn(ColumnList columnList) {
        QueryWrapper<ColumnList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", columnList.getName());
        //判断数据库中是否已经用了该栏目类型
        List<ColumnList> columnLists = columnMapper.selectList(queryWrapper);

        if (!columnLists.isEmpty()) {
            return false;
        }

        if (columnList.getPid() == null) {
            columnList.setSort(1);
            columnList.setStatus(true);
            columnList.setPid(0);
        }

        int i = columnMapper.insertColumn(columnList);
        return i > 0;

    }

    @Override
    public Boolean deleteColumn(IdsDao idsDao) {
        int i = columnMapper.deleteByIds(idsDao);
        return i > 0;
    }

    @Override
    public Boolean updateColumn(ColumnList columnList) {
        int i = columnMapper.updateById(columnList);
        return i > 0;
    }

    @Override
    public int getColumnNumber() {
        return columnMapper.getColumnNumber();
    }

    @Override
    public List<ColumnListVo> searchColumnByName(String name, Integer pid, Integer page, Integer size) {
        List<ColumnListVo> column = new ArrayList<>();
        int pageStart = (page - 1) * size;
        List<ColumnListVo> columnListVos = columnMapper.searchColumnByName(name, pid, pageStart, size);
        for (ColumnListVo columnVo : columnListVos) {
            List<ColumnListVo> children = getColumn(columnVo.getId(), page, size);
            columnVo.setChildren(children);
        }
        BeanUtil.copyProperties(columnListVos, column);

        return columnListVos;
    }

    @Override
    public int getColumnNumber1(String name, int pid) {
        return columnMapper.getColumnNumber1(name, pid);
    }
}
